package com.htudata.item.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import com.htudata.alarm.pojo.po.AlarmLevelPO;
import com.htudata.alarm.service.AlarmLevelService;
import com.htudata.common.service.TokenService;
import com.htudata.core.util.operation.CollectionsOperationUtils;
import com.htudata.core.util.operation.FastJsonOperationUtils;
import com.htudata.history.service.HisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class ItemSelectService2 {

  @Autowired
  private ItemSelectService itemSelectService;
  
  @Autowired
  private ItemKindService itemKindService;
  
  @Autowired
  private HisService hisService;
  
  @Autowired
  private AlarmLevelService alarmLevelService;
  
  @Autowired
  private HttpServletRequest request;
  
  @Value("${normal_level}")
  private int normalLevel;
  
  public List<Map<String, Object>> getItemsByNodeId(String nodeId, String a2) throws Exception {
    List<Long> userIds = null;
    long roleType = TokenService.getRoleType(this.request);
    if (roleType != -1L && roleType != 1L) {
      userIds = new ArrayList<>();
      userIds.add(Long.valueOf(TokenService.getUserId(this.request)));
    } 
    int isShowed = -1;
    int itemCount = 0;
    List<Map<String, Object>> result = this.itemKindService.getItemKindListWithoutCache(true);
    List<AlarmLevelPO> alarmLevelList = this.alarmLevelService.getAlarmLevelList(a2);
    JSONArray alarmDistributionData = new JSONArray();
    alarmLevelList.forEach(alarmLevel -> {
          JSONObject alarmLevelObject = new JSONObject(true);
          alarmLevelObject.put("name", alarmLevel.getName());
          alarmLevelObject.put("value", Integer.valueOf(0));
          alarmLevelObject.put("color", alarmLevel.getColor());
          alarmLevelObject.put("level", Integer.valueOf(alarmLevel.getLevel()));
          alarmDistributionData.add(alarmLevelObject);
        });
    JSONArray localItems = this.itemSelectService.getItems("-1", nodeId, 0, userIds, userIds);
    JSONArray hisJSONArray = new JSONArray();
    JSONArray hisStrJSONArray = new JSONArray();
    if (CollectionsOperationUtils.isValidCollection((Collection)localItems)) {
      hisJSONArray = this.hisService.getHisByItemId((List)localItems, "his");
      hisStrJSONArray = this.hisService.getHisByItemId((List)localItems, "his_str");
    } 
    JSONObject hisJSONObject = FastJsonOperationUtils.JSONArrayToJSONObejct(hisJSONArray, "item_id", false);
    JSONObject hisStrJSONObject = FastJsonOperationUtils.JSONArrayToJSONObejct(hisStrJSONArray, "item_id", false);
    JSONArray items = new JSONArray();
    if (CollectionsOperationUtils.isValidCollection((Collection)localItems))
      items.addAll((Collection)localItems); 
    FastJsonOperationUtils.sortByIntField(items, "alarm_level", "desc");
    JSONObject itemInfosJSONObject = null;
    JSONObject itemAndNode = null;
    JSONObject history = null;
    Map<String, Object> firstKindMap = null;
    Map<String, Object> secondKindMap = null;
    List<Map<String, Object>> firstKindList = null;
    List<Map<String, Object>> secondKindList = null;
    List<Map<String, Object>> nodeList = null;
    Map<String, Object> node = null;
    List<Map<String, Object>> itemList = null;
    for (Object obj : items) {
      itemAndNode = (JSONObject)obj;
      if (hisJSONObject.containsKey(itemAndNode.getString("item_id"))) {
        history = (JSONObject)hisJSONObject.get(itemAndNode.getString("item_id"));
      } else if (hisStrJSONObject.containsKey(itemAndNode.getString("item_id"))) {
        history = (JSONObject)hisStrJSONObject.get(itemAndNode.getString("item_id"));
      } else {
        history = null;
      } 
      int firstKind = itemAndNode.getIntValue("first_Kind");
      int secondKind = itemAndNode.getIntValue("second_Kind");
      for (int i = 0, length = result.size(); i < length; ) {
        firstKindMap = result.get(i);
        if (!(firstKind + "").equals((new StringBuilder()).append(firstKindMap.get("type")).append("").toString())) {
          i++;
          continue;
        } 
        firstKindList = (List<Map<String, Object>>)firstKindMap.get("list");
        for (int j = 0, length2 = firstKindList.size(); j < length2; ) {
          secondKindMap = firstKindList.get(j);
          if (!(secondKind + "").equals((new StringBuilder()).append(secondKindMap.get("type")).append("").toString())) {
            j++;
            continue;
          } 
          secondKindList = (List<Map<String, Object>>)firstKindMap.get("list");
          nodeList = (List<Map<String, Object>>)((Map)secondKindList.get(j)).get("list");
          int count = 0;
          for (int k = 0, length3 = nodeList.size(); k < length3; ) {
            node = nodeList.get(k);
            if (!itemAndNode.getString("node_nick_name").equals((new StringBuilder()).append(node.get("name")).append("").toString())) {
              k++;
              continue;
            } 
            itemList = (List<Map<String, Object>>)node.get("list");
            itemList.add(this.itemSelectService
                .getItemWithHistory(itemAndNode, history, alarmLevelList, isShowed, itemInfosJSONObject));
            firstKindMap.put("num", Integer.valueOf(Integer.parseInt((new StringBuilder()).append(firstKindMap.get("num")).append("").toString()) + 1));
            count++;
            setAlarmStatis(itemAndNode, alarmDistributionData);
            itemCount++;
          } 
          if (count == 0) {
            itemList = new ArrayList<>();
            itemList.add(this.itemSelectService
                .getItemWithHistory(itemAndNode, history, alarmLevelList, isShowed, itemInfosJSONObject));
            node = new HashMap<>();
            node.put("name", itemAndNode.getString("node_nick_name"));
            node.put("type", Integer.valueOf(itemAndNode.getIntValue("node_type_id")));
            node.put("state", Boolean.valueOf(true));
            node.put("list", itemList);
            nodeList.add(node);
            firstKindMap.put("num", Integer.valueOf(Integer.parseInt((new StringBuilder()).append(firstKindMap.get("num")).append("").toString()) + 1));
            setAlarmStatis(itemAndNode, alarmDistributionData);
            itemCount++;
          } 
        } 
      } 
    } 
    return result;
  }
  
  public void setAlarmStatis(JSONObject itemAndNode, JSONArray alarmDistributionData) {
    alarmDistributionData.forEach(alarmLevel -> {
          JSONObject alarmLevelObject = (JSONObject)alarmLevel;
          if (alarmLevelObject.getIntValue("level") == itemAndNode.getIntValue("alarm_level"))
            alarmLevelObject.put("value", Integer.valueOf(alarmLevelObject.getIntValue("value") + 1)); 
        });
  }
}
